/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mod.dao;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author Sandrine KONGUEM
 */
public class membre extends connexion implements database{
    private String PSEUDO,NOM,PRENOM,VILLE,PAYS,SEXE,PHOTO,PASSWORD;
    private String DATENAIS;
    private int MONTANT;
    private static String Table="membres";

    public membre(String PSEUDO, String PASSWORD) {
        this.PSEUDO = PSEUDO;
        this.PASSWORD = PASSWORD;
    }


    public membre(String PSEUDO, String NOM, String PRENOM, String VILLE, String PAYS, String SEXE, String DATENAIS,String Pass, int mont, String avatar) {
        this.PSEUDO = PSEUDO;
        this.NOM = NOM;
        this.PRENOM = PRENOM;
        this.VILLE = VILLE;
        this.PAYS = PAYS;
        this.SEXE = SEXE;
        this.DATENAIS = DATENAIS;
        this.PASSWORD=Pass;
        this.MONTANT=mont;
        this.PHOTO=avatar;
    }

    public membre() {
    }

    public String getDATENAIS() {
        return DATENAIS;
    }

    public void setDATENAIS(String DATENAIS) {
        this.DATENAIS = DATENAIS;
    }

    public String getNOM() {
        return NOM;
    }

    public void setNOM(String NOM) {
        this.NOM = NOM;
    }

    public String getPAYS() {
        return PAYS;
    }

    public void setPAYS(String PAYS) {
        this.PAYS = PAYS;
    }

    public String getPRENOM() {
        return PRENOM;
    }

    public void setPRENOM(String PRENOM) {
        this.PRENOM = PRENOM;
    }

    public String getPSEUDO() {
        return PSEUDO;
    }

    public void setPSEUDO(String PSEUDO) {
        this.PSEUDO = PSEUDO;
    }

    public String getSEXE() {
        return SEXE;
    }

    public void setSEXE(String SEXE) {
        this.SEXE = SEXE;
    }

    public String getVILLE() {
        return VILLE;
    }

    public void setVILLE(String VILLE) {
        this.VILLE = VILLE;
    }

    public String getPHOTO() {
        return PHOTO;
    }

    public void setPHOTO(String PHOTO) {
        this.PHOTO = PHOTO;
    }

    public int getMONTANT() {
        return MONTANT;
    }

    public void setMONTANT(int MONTANT) {
        this.MONTANT = MONTANT;
    }

    public static String getTable() {
        return Table;
    }

    public static void setTable(String Table) {
        membre.Table = Table;
    }

    public String getPASSWORD() {
        return PASSWORD;
    }

    public void setPASSWORD(String PASSWORD) {
        this.PASSWORD = PASSWORD;
    }


    @Override
    public boolean equals(Object obj) {
        if(obj instanceof membre)
        {
            return ((membre) obj).getPSEUDO().equals(this.getPSEUDO());
        }
        return false;
    }

    @Override
    public String toString() {
        return getNOM()+" "+getPRENOM();
    }

    public int Creation()
    {
        int v=0;
        String req="insert into "+membre.Table+"(pseudo,nom,prenom,datedenaissance,sexe,ville,pays,avatar,montant) " +
                "values('"+getPSEUDO()+"','"+getNOM()+"','"+getPRENOM()+"','"+getDATENAIS()+"','"+getSEXE()+"','"+getVILLE()+"','"+getPAYS()+"','"+getPHOTO()+"',"+getMONTANT()+")";
        v= Req_LMD(req);
        if(v==0)
        {
            v=this.Modification();
        }
        return v;
    }

    public int Modification()
    {
        int v=0;
        String req="upatde "+membre.Table+" set " +
                "nom ='"+getNOM()+"'"+
                "prenom ='"+getPRENOM()+"'"+
                "datedenaissance ='"+getDATENAIS()+"'"+
                "sexe ='"+getSEXE()+"'"+
                "ville ='"+getVILLE()+"'"+
                "pays ='"+getPAYS()+"'"+
                "avatar ='"+getPHOTO()+"'"+
                "montant ="+getMONTANT()+""+
                "WHERE pseudo='"+getPSEUDO()+"'";
        v= Req_LMD(req);
        return v;
    }

    public int Suppression() {
        int v=0;
        String req="delete from "+membre.Table+" where pseudo='"+getPSEUDO()+"'";
        v= this.Req_LMD(req);
        return v;
    }

    public ResultSet Liste()
    {
        ResultSet r=null;
        String req="select * from "+membre.Table;
        r=Req_LID(req);
        return r;
    }
    public membre getAuthentification()
    {

        String req="select * from "+membre.Table+" where pseudo ='"+getPSEUDO()+"' and password='"+getMd5(getPASSWORD())+"'";
        //System.out.println("req= "+req);
        ResultSet r=Req_LID(req);
        membre m=null;
        try {
            if(r!=null)if(r.next())
            {
                m=new membre(r.getString("pseudo"), r.getString("nom"), r.getString("prenom"), r.getString("ville"), r.getString("pays"), r.getString("sexe"), r.getString("datedenaissance"), r.getString("password"), r.getInt("montant"), r.getString("avatar"));
            }
        }catch(Exception e) {e.printStackTrace();}
        return m;
    }

    public static String getMd5(String password)
    {
        byte[] uniqueKey = password.getBytes();
        byte[] hash      = null;

        try
        {
            hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new Error("No MD5 support in this VM.");
        }

        StringBuilder hashString = new StringBuilder();
        for (int i = 0; i < hash.length; i++)
        {
            String hex = Integer.toHexString(hash[i]);
            if (hex.length() == 1)
            {
                hashString.append('0');
                hashString.append(hex.charAt(hex.length() - 1));
            }
            else
                hashString.append(hex.substring(hex.length() - 2));
        }
        return hashString.toString();
    }
}
